# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.


# def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
# print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
# if __name__ == '__main__':
#     print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
from lxml import etree
import requests
from utils import Utils


# 电视直播
class DianShiZhiBo(object):
    def __init__(self):
        self.encoding = "GB2312"
        self.url = "http://m.dianshizhibo.cc/"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Mobile Safari/537.36"
        }
        self.session = requests.session()

    def __get_data(self):
        request = self.session.get(self.url, headers=self.headers)
        request.encoding = self.encoding
        return request.text

    def __parse_data(self, html_data):
        data = []
        html = etree.HTML(html_data)
        result = html.xpath("//div[@class='con']/ul/li/a")
        for item in result:
            zhiboyuan = {
                "url": self.url + item.xpath("./@href")[0],
                "name": item.xpath("./text()")[0]
            }
            data.append(zhiboyuan)
        return data

    def __get_live_source(self, data):
        live_sources = []
        for item in data:
            result = self.session.get(item['url'])
            result.encoding = self.encoding
            html = etree.HTML(result.text)
            temp = html.xpath("//iframe[@scrolling='no']/@src")
            if temp:
                video_html = temp[0]
                video = self.session.get(video_html)
                video_ulr = etree.HTML(video.text)
                temp = video_ulr.xpath('//video/@src')
                if temp:
                    live_source = {
                        'url': temp[0],
                        'name': item['name'].replace('-', '').replace(' ', '').upper()
                    }
                    live_sources.append(live_source)
        live_sources.sort(key=lambda x: (x['name'][0], x['name'][1], x['name'][2]))
        print(live_sources)
        return live_sources

    def get_live_sources(self):
        text = self.__get_data()
        data = self.__parse_data(text)
        return self.__get_live_source(data)

    def run(self):
        text = self.__get_data()
        data = self.__parse_data(text)
        Utils.write_file(self.__get_live_source(data))

# if __name__ == '__main__':
#     zhi = DianShiZhiBo()
#     zhi.run()
#     # 'cctv3', '海南卫视', 'cctv12', 'cctv14', '湖南卫视', '广东卫视', '广西卫视'
#     # student = [['Tom', 'A', 20], ['Jack', 'C', 18], ['Andy', 'B', 11]]
#     # student.sort(key=lambda student: student[2])
#     # print(student)
